/**
 * Created by Administrator on 2016/10/5.
 */
var exchange = (function($){
    return{

        tableObj : null,
        otherElem : {},
        // 初始化
        init: function(){
            momo.initPage();// 左边菜单栏
           // momo.download.call(this, "PUser/export");// 导出
            this.addBtnEvent();// 给“搜索”“重置”按钮添加点击事件

            //动态接受参数进行设置
            var isIFrame = momo.getURLElement("isIFrame");// 是否是模态框
            //console.log( isIFrame );

            if( isIFrame ) { // 是模态框

                // iframe内部页面 移除 头部、左边菜单栏
                $('#header').remove();
                $('#sidebar').remove();
                $('.breadcrumbs').remove();
                $('.page-header').remove();
                $('.page-content').css('border', 'none');

                // 根据搜索条件初始化表格数据
                var name = $('#searchName').val();
                var type = $('#searchType').val();
             //   var account = $('#searchAccount').val();

                var _data = {
                    "name": name,
                    "type": type,
                //    "account": account,
                };
                this.initTable( _data, true );// 初始化表格

            }else{
                this.initTable( {}, false );// 初始化表格
            }

            // 导出
            this.otherElem = { };
        },

        // 初始化表格
        initTable: function( data, isIFrame ){
            console.log(data);
            var _self = this;

            var url = 'PExchange/index';
            var editUrl = '';
            var colName = ['序号','礼品名称','礼品类型','总库存','剩余数量','兑换积分','是否为实物','是否显示','添加时间','操作'];
            var colModal = [
                { index: 'id', width:5, textOverflowEllipsis: true, sort: false, formatter: setNum},
                { index: 'name', width:20, textOverflowEllipsis: true, sort: false },
                { index: 'type', width:15, textOverflowEllipsis: true, sort: false },
                { index: 'total', width:20, textOverflowEllipsis: true, sort: false },
                { index: 'sku', width:10, textOverflowEllipsis: true, sort: false },
                { index: 'score', width:10, textOverflowEllipsis: true, sort: false },
                { index: 'material', width:15, textOverflowEllipsis: true, sort: false },
                { index: 'is_show', width:10, textOverflowEllipsis: true, sort: false },
                { index: 'add_time', width:20, textOverflowEllipsis: true, sort: false },
                { index: 'id', width:20, textOverflowEllipsis: true, sort: false, formatter: setOperate.bind(_self) },// 传入id，操作表单
            ];

            // 如果是iframe，不显示“操作”
            if( isIFrame ){
                colName.pop();
                colModal.pop();
            }

            var _data = {};
            if( data != undefined ) _data = $.extend( _data, data );
            this.tableObj = new $.jqGrid( $('#jqGrid'), url, _data, colName, colModal, undefined, false, true, editUrl );
             console.log(this.tableObj);


            // （辅助函数）设置序号
            function setNum( val, rowData, index ) {
                var _num = ( _self.tableObj.currentPage - 1 ) *  _self.tableObj.data.page_size + ( index + 1 );
                return '<span>' + parseInt( _num ) + '</span>';
            }  
            // （辅助函数）设置操作（this是 lottery对象）
            function setOperate( val, rowData ){
               var _str =  '<a class="updateItem btn btn-mini btn-primary tableInnerBtn" onclick="exchange.top(\'' + val + '\');">置顶</a>';
               _str +=  '<a class="updateItem btn btn-mini btn-primary tableInnerBtn" onclick="exchange.edit(\'' + val + '\');">编辑</a>';
               _str +=  '<a class="updateItem btn btn-mini btn-primary tableInnerBtn" onclick="exchange.del(\'' + val + '\');">删除</a>';
                //if( momo.getURLElement("isIFrame") ) return '';// 是模态框，不允许操作
                return _str;
            }
        },

        top:function(id){
            var _self = this;
            momo.sendPost({id:id},"PExchange/top", function(res){
                if (res.errcode == 0) {
                    alert("置顶成功");
                    var name = $('#searchName').val();
                    var type = $("#searchType").val();
                    var _data = {
                        "name": name,
                        "type": type
                    };
                    _self.tableObj.reloadData( _data, true );
                }else{
                    alert("置顶失败");
                }
            })
        },

        edit: function(id){
            window.location.href = 'editGift.html?id='+id;
        },

        view:function(id){
            window.location.href = 'view.html?id='+id;
        },
        del:function(id){
            var _self = this;
            if(confirm("确认删除？")){
                momo.sendPost({id:id},"PExchange/del", function(res){
                    if (res.errcode == 0) {
                        alert("删除成功");
                        var name = $('#searchName').val();
                        var type = $("#searchType").val();
                        var _data = {
                            "name": name,
                            "type": type
                        };
                        _self.tableObj.reloadData( _data, true );
                    }else{
                        alert("删除失败");
                    }
                })
            }
        },

        // 给“搜索”“重置”按钮、“展开/收起日志”、“取消订单”按钮、订单备注编辑、保存、取消按钮 添加点击事件
        addBtnEvent: function(){

            var _self = this;

            // 点击搜索按钮
            $('.searchBtn').click( function(){

                var name = $('#searchName').val();
               var type = $("#searchType").val();
                var _data = {
                    "name": name,
                    "type": type
                };
                //console.log(_data);
                this.tableObj.reloadData( _data, true );

                this.otherElem = $.extend( this.otherElem, _data);// 导出功能

            }.bind( this ));

            // 点击重置按钮
            $('.resetBtn ').click( function(){
                $("#searchName").val('');

            });       

        },
    }
})(jQuery);

var init = exchange.init.bind( exchange );
